import { TileList } from "../../../fl/controls/TileList";
import { IFrUI } from "../core/IFrUI";
import { InvalidationType } from "../../../fl/core/InvalidationType";
import { SkinManager } from "../skins/SkinManager";
import { ScrollBarDirection } from "../../../fl/controls/ScrollBarDirection";
import { ICellRenderer } from "../../../fl/controls/listClasses/ICellRenderer";
import { TileListData } from "../../../fl/controls/listClasses/TileListData";
import { ListData } from "../../../fl/controls/listClasses/ListData";
import { UIComponent } from "../../../fl/core/UIComponent";
import { DataProvider } from "../../../fl/data/DataProvider";
import { ObjEventListerTool } from "../utils/ObjEventListerTool";
type int = number;
//class GTileList
    
    export  class GTileList extends TileList implements IFrUI
    {
       

        public set horizontalGap(arg1: number)
        {
            this._horizontalGap = arg1;
            return;
        }

        public get horizontalGap(): number
        {
            return this._horizontalGap;
        }

        public set verticalGap(arg1: number)
        {
            this._verticalGap = arg1;
            return;
        }

        public get verticalGap(): number
        {
            return this._verticalGap;
        }

        public setVerticalGapAt(arg1: int, arg2: number): void
        {
            if (this._arrVGap == null) 
            {
                this._arrVGap = [];
            }
            this._arrVGap[arg1] = arg2;
            return;
        }

        public setHorizontalGapAt(arg1: int, arg2: number): void
        {
            if (this._arrHGap == null) 
            {
                this._arrHGap = [];
            }
            this._arrHGap[arg1] = arg2;
            return;
        }

        /* internal  */getTotalGap(arg1: int, arg2: Array<any>, arg3: int): int
        {
            var loc1=0;
            if (arg2 == null) 
            {
                return arg1 * arg3;
            }
            var loc2=0;
            while (loc2 < arg1) 
            {
                if (arg2[loc2] != null) 
                {
                    loc1 = loc1 + int(arg2[loc2]);
                }
                else 
                {
                    loc1 = loc1 + arg3;
                }
                ++loc2;
            }
            return loc1;
        }

        public get styleName(): string
        {
            return this._styleName;
        }

        public set styleName(arg1: string)
        {
            if (this._styleName != arg1) 
            {
                this._styleName = arg1;
                invalidate(InvalidationType.STYLES);
                this._isStyleChange = true;
            }
            return;
        }

        protected final override draw(): void
        {
            if (isInvalid(InvalidationType.STYLES)) 
            {
                if (this._isStyleChange) 
                {
                    SkinManager.setComponentStyle(this, this._styleName);
                    this._isStyleChange = false;
                }
            }
            this._isDraw = true;
            super.draw();
            this._isDraw = false;
            return;
        }

        public set useFixed(arg1: Boolean)
        {
            this._isUseFixed = arg1;
            return;
        }

        protected /* override */ drawList(): void
        {
            var loc1=0;
            var loc2=0;
            var loc3=null;
            var loc4=null;
            var loc11=0;
            var loc12=0;
            var loc15=null;
            var loc16=0;
            var loc17=0;
            var loc18=0;
            var loc19=0;
            var loc20=false;
            var loc21=null;
            var loc22=null;
            var loc23=null;
            var loc24=null;
            var loc25=null;
            var loc26=null;
            var loc5=rowCount;
            var loc6=columnCount;
            var loc7=columnWidth;
            var loc8=rowHeight;
            var loc9=0;
            var loc10=0;
            var loc27;
            listHolder.y = loc27 = contentPadding;
            listHolder.x = loc27;
            contentScrollRect = listHolder.scrollRect;
            contentScrollRect.x = Math.floor(_horizontalScrollPosition) % loc7;
            contentScrollRect.y = Math.floor(_verticalScrollPosition) % loc8;
            listHolder.scrollRect = contentScrollRect;
            listHolder.cacheAsBitmap = useBitmapScrolling;
            var loc13=[];
            if (_scrollDirection != ScrollBarDirection.HORIZONTAL) 
            {
                ++loc5;
                loc10 = _verticalScrollPosition / loc8 << 0;
                if (this._isUseFixed) 
                {
                    loc10 = _verticalScrollPosition / (loc8 + this.verticalGap) << 0;
                }
                loc18 = Math.floor(loc10 * loc6);
                loc19 = Math.min(length, loc18 + loc5 * loc6);
                loc1 = loc18;
                while (loc1 < loc19) 
                {
                    loc13.push(loc1);
                    ++loc1;
                }
            }
            else 
            {
                loc16 = availableWidth / loc7 << 0;
                if (this._isUseFixed) 
                {
                    loc16 = (availableWidth + this.horizontalGap) / (loc7 + this.horizontalGap) << 0;
                }
                loc17 = Math.max(loc16, Math.ceil(length / loc5));
                loc9 = _horizontalScrollPosition / loc7 << 0;
                if (this._isUseFixed) 
                {
                    loc9 = _horizontalScrollPosition / (loc7 + this.horizontalGap) << 0;
                }
                loc6 = Math.max(loc16, Math.min(loc17 - loc9, loc6 + 1));
                loc12 = 0;
                while (loc12 < loc5) 
                {
                    loc11 = 0;
                    while (loc11 < loc6) 
                    {
                        loc2 = loc12 * loc17 + loc9 + loc11;
                        if (loc2 >= length) 
                        {
                            break;
                        }
                        loc13.push(loc2);
                        ++loc11;
                    }
                    ++loc12;
                }
            }
            renderedItems = loc27 = new Map<any, any> /* flash.utils.Dictionary */(true);
            var loc14=loc27;
            loc27 = 0;
            var loc28=loc13;
            for(loc2 of loc28) 
            {
                loc14[_dataProvider.getItemAt(loc2)] = true;
            }
            loc15 = new Map<any, any> /* flash.utils.Dictionary */(true);
            while (activeCellRenderers.length > 0) 
            {
                loc3 = (loc4 = activeCellRenderers.pop()).data;
                if (loc14[loc3] == null || invalidItems[loc3] == true) 
                {
                    availableCellRenderers.push(loc4);
                }
                else 
                {
                    loc15[loc3] = loc4;
                    invalidItems[loc3] = true;
                }
                list.removeChild(loc4 as Object /* flash.display.DisplayObject */);
            }
            invalidItems = new Map<any, any> /* flash.utils.Dictionary */(true);
            loc1 = 0;
            loc27 = 0;
            loc28 = loc13;
            for(loc2 of loc28) 
            {
                loc11 = loc1 % loc6;
                loc12 = loc1 / loc6 << 0;
                loc20 = false;
                loc3 = _dataProvider.getItemAt(loc2);
                if (loc15[loc3] == null) 
                {
                    if (availableCellRenderers.length > 0) 
                    {
                        loc4 = availableCellRenderers.pop() as ICellRenderer;
                    }
                    else if ((loc24 = (loc4 = getDisplayObjectInstance(getStyleValue("cellRenderer")) as ICellRenderer) as Object /* flash.display.Sprite */) != null) 
                    {
                        loc24.addEventListener(flash.events.MouseEvent.CLICK, handleCellRendererClick, false, 0, true);
                        loc24.addEventListener(flash.events.MouseEvent.ROLL_OVER, handleCellRendererMouseEvent, false, 0, true);
                        loc24.addEventListener(flash.events.MouseEvent.ROLL_OUT, handleCellRendererMouseEvent, false, 0, true);
                        loc24.addEventListener(flash.events.Event.CHANGE, handleCellRendererChange, false, 0, true);
                        loc24.doubleClickEnabled = true;
                        loc24.addEventListener(flash.events.MouseEvent.DOUBLE_CLICK, handleCellRendererDoubleClick, false, 0, true);
                        if (loc24.hasOwnProperty("setStyle")) 
                        {
                            var loc29=0;
                            var loc30=rendererStyles;
                            for (loc25 in loc30) 
                            {
                                var loc31;
                                (loc31 = loc24)["setStyle"](loc25, rendererStyles[loc25]);
                            }
                        }
                    }
                }
                else 
                {
                    loc20 = true;
                    loc4 = loc15[loc3];
                    delete loc15[loc3];
                }
                list.addChild(loc4 as Object /* flash.display.Sprite */);
                activeCellRenderers.push(loc4);
                loc4.y = loc8 * loc12 + this.getTotalGap(loc12, this._arrVGap, this._verticalGap);
                loc4.x = loc7 * loc11 + this.getTotalGap(loc11, this._arrHGap, this._horizontalGap);
                loc4.setSize(columnWidth, rowHeight);
                loc21 = this.itemToLabel(loc3);
                loc22 = null;
                if (_iconFunction == null) 
                {
                    if (!(_iconField == null) && loc3 && loc3.hasOwnProperty(_iconField)) 
                    {
                        loc22 = loc3[_iconField];
                    }
                }
                else 
                {
                    loc22 = _iconFunction(loc3);
                }
                loc23 = null;
                if (_sourceFunction == null) 
                {
                    if (!(_sourceField == null) && loc3 && loc3.hasOwnProperty(_sourceField)) 
                    {
                        loc23 = loc3[_sourceField];
                    }
                }
                else 
                {
                    loc23 = _sourceFunction(loc3);
                }
                if (!loc20) 
                {
                    loc4.data = loc3;
                }
                loc4.listData = new TileListData(loc21, loc22, loc23, this, loc2, loc10 + loc12, loc9 + loc11) as ListData;
                loc4.selected = !(_selectedIndices.indexOf(loc2) == -1);
                if (typeof loc4 === "uicomponent") 
                {
                    (loc26 = loc4 as UIComponent).drawNow();
                }
                ++loc1;
            }
            return;
        }

        public /* override */ set dataProvider(arg1: DataProvider)
        {
            var loc1=0;
            var loc2=0;
            var loc3=null;
            var loc4=null;
            if (super.dataProvider) 
            {
                loc1 = Math.max(super.dataProvider.length, arg1.length);
                loc2 = 0;
                while (loc2 < loc1) 
                {
                    if (loc2 > (super.dataProvider.length - 1) && loc2 <= (arg1.length - 1)) 
                    {
                        super.dataProvider.addItemAt(arg1.getItemAt(loc2), loc2);
                    }
                    else if (loc2 > (arg1.length - 1)) 
                    {
                        super.dataProvider.removeItemAt(arg1.length);
                    }
                    else 
                    {
                        loc3 = this.dataProvider.getItemAt(loc2);
                        loc4 = arg1.getItemAt(loc2);
                        super.dataProvider.replaceItemAt(loc4, loc2);
                        invalidateItemAt(loc2);
                    }
                    ++loc2;
                }
            }
            else 
            {
                super.dataProvider = arg1;
            }
            return;
        }

        public /* override */ itemToLabel(arg1: Object): string
        {
            if (_labelFunction != null) 
            {
                return String(_labelFunction(arg1));
            }
            if (_labelField == null || arg1 == null || arg1.hasOwnProperty(_labelField) == false) 
            {
                return "";
            }
            return String(arg1[_labelField]);
        }

        protected /* override */ _invalidateList(): void
        {
            var loc1=0;
            if (this._isDraw) 
            {
                super._invalidateList();
            }
            else 
            {
                loc1 = 0;
                while (loc1 < _dataProvider.length) 
                {
                    invalidateItem(_dataProvider.getItemAt(loc1));
                    ++loc1;
                }
                invalidate(InvalidationType.DATA);
            }
            return;
        }

        protected /* override */ keyDownHandler(arg1: Object /* flash.events.KeyboardEvent */): void
        {
            return;
        }

        protected /* override */ keyUpHandler(arg1: Object /* flash.events.KeyboardEvent */): void
        {
            return;
        }

        public configEventListener(arg1: string, arg2: Function, arg3: Boolean=false, arg4: int=0, arg5: Boolean=false): void
        {
            ObjEventListerTool.addObjEvent(this, arg1, arg2, arg3);
            addEventListener(arg1, arg2, arg3, arg4, arg5);
            return;
        }

        public /* override */ removeEventListener(arg1: string, arg2: Function, arg3: Boolean=false): void
        {
            ObjEventListerTool.removeObjEvent(this, arg1, arg2, arg3);
            super.removeEventListener(arg1, arg2, arg3);
            return;
        }

        public dispose(arg1: Boolean=true): void
        {
            return;
        }

        private /* var */_verticalGap: number=0;

        private /* var */_horizontalGap: number=0;

        private /* var */_arrVGap: Array<any>;

        private /* var */_arrHGap: Array<any>;

        private /* var */_styleName: string;

        private /* var */_isStyleChange: Boolean=false;

        private /* var */_isUseFixed: Boolean=false;

        private /* var */_isDraw: Boolean=false;
    }
